<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                @click="hAdd"
                icon="el-icon-plus"
                size="small"
                type="primary"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button @click="hAssignPer(row.id)" size="small" type="success">分配权限</el-button>
                  <el-button @click="hEdit(row)" size="small" type="primary"
                    >编辑</el-button
                  >
                  <el-button @click="hDel(row.id)" size="small" type="danger"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageParams.page"
                :page-sizes="[2, 4, 5, 10, 20]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        @closed="restForm"
        :title="isEdit ? '编辑角色' : '新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog >
      <!-- 角色分配权限弹层 -->
      <el-dialog title="角色分配权限" :visible.sync="showAssignPerDialog">
        <assignPer @close="showAssignPerDialog=false" v-if="showAssignPerDialog" :curId="curId" />
      </el-dialog>
    </div>
  </div>
</template>


<script>
import { addRoles, delRoles, getRoles, updateRoles } from "@/api/settings";
import assignPer from './assignPermission'
export default {
  components:{
    assignPer
  },
  data() {
    return {
      curId:'',
      showAssignPerDialog:false,
      pageParams: {
        page: 1,
        pagesize: 2,
      },
      showDialog: false, //默认关闭弹层
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入角色的名称", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入角色的描述", trigger: "blur" },
        ],
      },
      list: [],
      total: 0, //总数
      isEdit: false, //默认值为添加
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    hAssignPer(id){
      this.curId=id
      this.showAssignPerDialog=true
    },
    //删除功能
    hDel(id) {
      this.$confirm("确定要删除此角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delRoles(id);
          if (res.code !== 10000) return this.$message.error(res.message);
          this.$message.success(res.message);
          if(this.list.length===1){
            this.pageParams.page--
            if(this.pageParams.page<=0){
              this.pageParams.page=1
            }
          }
          this.loadRoles();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //关闭弹层重置表单
    restForm() {
      this.$refs.roleForm.resetFields();
      this.roleForm = {
        name: "",
        description: "",
      };
    },
    hEdit(row) {
      this.roleForm = { ...row };
      this.showDialog = true;
      this.isEdit = true;
    },
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return;
        this.isEdit ? this.doEdit() : this.doAdd();
      });
    },
    hAdd() {
      this.showDialog = true;
      this.isEdit = false;
    },
    async doEdit() {
      try {
        const res = await updateRoles(this.roleForm);
        // console.log(res);
        if (res.code !== 10000) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.loadRoles();
        this.showDialog = false;
      } catch (error) {
        console.log(error);
      }
    },
    async doAdd() {
      try {
        const res = await addRoles(this.roleForm);
        // console.log(res);
        if (res.code !== 10000) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.total++;
        const maxPage = Math.ceil(this.total / this.pageParams.pagesize);
        this.pageParams.page = maxPage;
        this.loadRoles();
        this.showDialog = false;
      } catch (error) {
        console.log(error);
      }
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams);
        this.list = res.data.rows;
        this.total = res.data.total;
      } catch (error) {
        console.log(error);
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageParams.page = 1;
      this.pageParams.pagesize = val;
      this.loadRoles();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageParams.page = val;
      this.loadRoles();
    },
  },
};
</script>
